import config from './config';
import { logService } from '../service/log';
const SMSClient = require('@alicloud/sms-sdk')
var xml2json = require("xmlparser");

/**
 * 短信
 */
class SMS {
    async sendmsg(validation: String, phone: String, type?: Number) {
        const sms = config.sms;
        const accessKeyId = sms.AccessKeyID;
        const secretAccessKey = sms.AccessKeySecret;
        const originname = sms.originname;
        const TemplateCode = sms.TemplateCode;
        //初始化sms_client
        let smsClient = new SMSClient({ accessKeyId, secretAccessKey })
        //发送短信
        console.log(phone + '短信验证码为：' + validation);
        smsClient.sendSMS({
            PhoneNumbers: phone,
            SignName: originname,
            TemplateCode: TemplateCode,
            TemplateParam: `{"code":${validation}}`
        }).then((res: any) => {
            console.log(res)
            res.phone = phone;
            res.validation = validation;
            try {
                logService.create({
                    applicationName: "scyy.app",
                    type: 'info',
                    module: 'sms',
                    title: 'sms',
                    detail: res,
                    success: true
                });
            } catch (err) {
                console.log('警告', '发送短信写日志失败', err);
            }
            return res;
        }, (err: any) => {
            console.log(err)
            return err;
        })
    }
    async sendmsg2(name: String, store: String, phone: String, winename: String) {
        const sms = config.sms;
        const accessKeyId = sms.AccessKeyID;
        const secretAccessKey = sms.AccessKeySecret;
        const originname = sms.originname;
        const TemplateCode = 'SMS_172882139';
        //初始化sms_client
        let smsClient = new SMSClient({ accessKeyId, secretAccessKey })
        //发送短信
        console.log(name + '---' + store + '[' + winename + ']' + '酒水不足提醒：');
        smsClient.sendSMS({
            PhoneNumbers: phone,
            SignName: originname,
            TemplateCode: TemplateCode,
            TemplateParam: `{"name":"${name}","store": "${store}(${winename})"}`
        }).then((res: any) => {
            console.log(res)
            res.phone = phone;
            res.validation = `{"name":${name}, "store": ${store}(${winename})}`;
            try {
                logService.create({
                    applicationName: "scyy.app",
                    type: 'info',
                    module: 'sms',
                    title: 'sms',
                    detail: res,
                    success: true
                });
            } catch (err) {
                console.log('警告', '发送短信提醒', err);
            }
            return res;
        }, (err: any) => {
            console.log(err)
            return err;
        })
    }
    async sendmsg3(phone: String, merchantname: String, customername: String, paytime: String, paymoney: String, paydelail: any) {
        const sms = config.sms;
        const accessKeyId = sms.AccessKeyID;
        const secretAccessKey = sms.AccessKeySecret;
        const originname = sms.originname;
        const TemplateCode = 'SMS_179220740';
        let smsClient = new SMSClient({ accessKeyId, secretAccessKey })
        //发送短信
        //尊敬的${merchantname},顾客${customername}于${paytime}消费金额${paymoney}.消费详情${paydelail}
        console.log(`商户订单支付成功提醒短信内容： 尊敬的${merchantname},顾客${customername}于${paytime}消费金额${paymoney}.消费详情${paydelail}`);
        smsClient.sendSMS({
            PhoneNumbers: phone,
            SignName: originname,
            TemplateCode: TemplateCode,
            TemplateParam: `{"merchantname":"${merchantname}","customername": "${customername}","paytime":"${paytime}","paymoney": "${paymoney}","paydelail": "${paydelail}"}`
        }).then((res: any) => {
            console.log(res)
            res.phone = phone;
            res.validation = `{"merchantname":"${merchantname}","customername": "${customername}","paytime":"${paytime}","paymoney": "${paymoney}","paydelail": "${paydelail}"}`;
            try {
                logService.create({
                    applicationName: "scyy.app",
                    type: 'info',
                    module: 'sms',
                    title: '商家订单支付提醒',
                    detail: res,
                    success: true
                });
            } catch (err) {
                console.log('警告', '发送短信提醒', err);
            }
            return res;
        }, (err: any) => {
            console.log(err)
            return err;
        })
    }
}

export const sms = new SMS();
